<template>
  <div class="register">
    <div class="container">
      <div class="close">
        <span class="iconfont iconicon-test"></span>
      </div>
      <div class="logo">
        <span class="iconfont iconnew"></span>
      </div>
      <div class="inputs">
        <hminput
          placeholder="请输入手机号"
          type="text"
          v-model="user.username"
          :rules="/^1[35789]\d{9}$/"
          errmsg="请输入正确的手机号！"
        ></hminput>
        <hminput
          placeholder="密码"
          type="password"
          v-model="user.password"
          :rules="/^\S{3,16}$/"
          errmsg="请输入3-16位的密码！"
        ></hminput>
        <hminput
          placeholder="昵称"
          type="text"
          v-model="user.nickname"
          errmsg="请输入您的昵称"
        ></hminput>
      </div>
      <p class="tips">
        已有账号？
        <a href="/login">去登陆</a>
      </p>
      <hmbutton type="default" @click="handleRegister">注册</hmbutton>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
import hmbutton from "@/components/hmbutton.vue";
import hminput from "@/components/hminput.vue";
import { register } from "@/apis/user.js";
export default {
  components: {
    hmbutton,
    hminput,
  },
  data() {
    return {
      user: {
        username: "",
        password: "",
        nickname: "",
      },
    };
  },
  methods: {
    async handleRegister() {
      if (
        /^1[35789]\d{9}$/.test(this.user.username) &&
        /^\S{3,16}$/.test(this.user.password) &&
        this.user.nickname
      ) {
        try {
          let res = await register(this.user);
          console.log(res);
          // 注册成功
          if (res.status == 200) {
            Toast.success({
              message: res.data.message,
              duration: 2000,
            });
            this.$router.push({ name: "login" });
          } else {
            Toast.fail("注册失败！");
          }
        } catch {
          Toast.fail("注册失败！");
        }
      } else {
        Toast.fail("请输入符合规范的数据！");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
}
.close {
  span {
    font-size: 27 * 100vw/360;
  }
}
.logo {
  display: flex;
  justify-content: center;
  span {
    display: block;
    color: #d81e06;
    font-size: 126 * 100vw/360;
  }
}
.inputs {
  input {
    margin-bottom: 20px;
  }
}
.tips {
  margin-top: 20px;
  margin-bottom: 20px;
  text-align: right;
  a {
    color: #3385ff;
  }
}
</style>